package com.ygxsk.carhome;

import android.app.IntentService;
import android.content.Intent;

import androidx.annotation.Nullable;

import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsListener;

import java.util.HashMap;

/**
 * Created by itchenqi on 2018/4/22 0022.
 * description：腾讯X5的预加载内容,开启首页的时候，这个貌似不起作用加载非常慢
 */
public class X5CorePreLoadService extends IntentService {

    //private static final String TAG = X5CorePreLoadService.class.getSimpleName();
    private static final String TAG = "itchen";

    public X5CorePreLoadService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        //LogUtils.d("itchen-----------onHandleIntent------------执行");
        //在这里添加我们要执行的代码，Intent中可以保存我们所需的数据，
        //每一次通过Intent发送的命令将被顺序执行
        initX5();

    }

    private void initX5() {

        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);

        //QbSdk.setNeedInitX5FirstTime(true);//20190815升级之后不可用
        QbSdk.setDownloadWithoutWifi(true);
        QbSdk.canLoadX5FirstTimeThirdApp(this.getApplicationContext());

        QbSdk.initX5Environment(getApplicationContext(), cb);

        //LogUtils.d("itchen--初始化x5环境--initX5Environment");
        //=========================================================================

        //预加载=========================================================================
        if (!QbSdk.isTbsCoreInited()) {
            //LogUtils.d("itchen--未初始化QbSdk---开始执行重新初始化");
            QbSdk.preInit(getApplicationContext(), cb);
            // 设置X5初始化完成的回调接口
            //X5webview的preinit接口只是降低了webview的冷启动时间
            //preinit只需要调用一次，如果已经完成了初始化，那么就直接构造view

        } else {
            //LogUtils.d("itchen--QbSdk已经初始化成功");
        }
        //=========================================================================
        // 搜集本地tbs内核信息并上报服务器，服务器返回结果决定使用哪个内核。
        //TbsDownloader.needDownload(getApplicationContext(),false);

        //TbsDownloader.needDownload()
        QbSdk.setTbsListener(new TbsListener() {

            @Override
            public void onDownloadFinish(int i) {
                //LogUtils.d("itchen---onDownloadFinish--->" + i);
                //TbsHandlerThread, com.dgj.dinggovern.X5CorePreLoadService$1.onDownloadFinish(X5CorePreLoadService.java:74)
                //    itchen---onDownloadFinish--->110
                //itchen---onDownloadFinish--->100 代表下载完毕。
            }

            @Override
            public void onInstallFinish(int i) {
                //LogUtils.d("itchen---onInstallFinish--->" + i);
                //TbsHandlerThread, com.dgj.dinggovern.X5CorePreLoadService$1.onInstallFinish(X5CorePreLoadService.java:79)
                //itchen---onInstallFinish--->200

                //TbsHandlerThread, com.dgj.dinggovern.X5CorePreLoadService$1.onInstallFinish(X5CorePreLoadService.java:79)
                //itchen---onInstallFinish--->232
            }

            @Override
            public void onDownloadProgress(int i) {

                //LogUtils.d("itchen---onDownloadProgress--->" + i);

            }
        });


    }

    /**
     * 设置回调 设置X5初始化完成的回调接口
     */
    QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

        @Override
        public void onViewInitFinished(boolean arg0) {
            // //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
            //LogUtils.d("itchen--X5CorePreLoadService--onViewInitFinished-->"+arg0);
            //new WebView(X5CorePreLoadService.this);
        }

        @Override
        public void onCoreInitFinished() {
            // retain
            //LogUtils.d("itchen---X5CorePreLoadService---onCoreInitFinished");
            //QbSdk.initX5Environment(getApplicationContext(), null);
        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        //LogUtils.d("itchen--X5CorePreLoadService---onDestroy");
    }
}
